﻿using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    /// <summary>
    /// 用户操作
    /// </summary>
    public class UserOptController : ApiController
    {
        /// <summary>
        /// 获取用户列表，主要用于对用户增删改之前的数据展示以及用户查询
        /// eg. :0000000001|TST1||12345678901||WEB|
        /// </summary>
        /// <param name="key">memberid|comcd（空值=所有）|deptcd（空值=所有）|手机号（空值=所有）|userid（空值=所有）|登录平台（WEB/PC/APP）|指定的memberid（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/useropt")]
        public RespSysUserInfo GetUserList(string key)
        {
            RespSysUserInfo resp = new RespSysUserInfo();

            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 7)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strDeptcd = strsKeyOriginal[2];
            string strMpno = strsKeyOriginal[3];
            string strUserid = strsKeyOriginal[4];
            string strPlatcd = strsKeyOriginal[5];
            string strMemberidQuery = strsKeyOriginal[6];

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = userLogic.GetComUsers(strComcd, strDeptcd, strMpno, strUserid,strMemberidQuery);
            return resp;
        }

        /// <summary>
        /// 修改用户常规信息，不能操作冻结标志等敏感字段
        /// </summary>
        /// <param name="_reqInfo"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/useropt")]
        public RespSysNullResultInfo UpdateUserInfo(ReqUserInfo _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();

            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken,_reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            UserInfo_Modify collecUser = _reqInfo.userinfo;
            switch (collecUser.optlb.ToUpper())
            {
                case "I": //管理员增加用户
                    string strPassEncrypt = DataUtil.EncryptPassword("123456");
                    if (strPassEncrypt == "")
                    {
                        resp.statusCode = 10006;
                        resp.statusMsg = "密码校验失败";
                        return resp;
                    }
                    string strMemberidNew = userLogic.RegMember(collecUser.mpno, strPassEncrypt, collecUser.username, collecUser.userid, collecUser.deptcd);
                    if (strMemberidNew == "0")
                    {
                        resp.statusCode = 10004;
                        resp.statusMsg = "用户已存在";
                        return resp;
                    }
                    else if (strMemberidNew == "")
                    {
                        resp.statusCode = 10025;
                        resp.statusMsg = "用户注册失败";
                        return resp;
                    }

                    resp.statusCode = 0;
                    resp.statusMsg = "操作成功";
                    break;
                case "U": //
                    resp = userLogic.UpdateUserInfo(collecUser);
                    break;
                default:
                    resp.statusCode = 90014;
                    resp.statusMsg = "操作标识异常";
                    break;
            }

            return resp;
        }

        /// <summary>
        /// 用户自助修改密码
        /// </summary>
        /// <param name="_reqInfo">自助修改密码请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/changepwd")]
        public RespSysNullResultInfo ChangePassword(ReqSelfChangePwd _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            if (_reqInfo.originpwd == _reqInfo.newpwd)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "新旧密码不能一致";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = userLogic.ChangeSelfPassword(_reqInfo.memberid, _reqInfo.originpwd, _reqInfo.newpwd);
            return resp;
        }


    }
}
